package ch.unisi.inf.pfii.teamred.pacman.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import ch.unisi.inf.pfii.teamred.pacman.model.Game;

public final class PacmanFrame extends JFrame {

	private GameView gameView;
	private final HighscoreView highscoreView;
	private boolean firstTimeStart;
	private final Game game;

	/**
	 * This class models the window view of the pacman game.
	 * 
	 * @author mark.pruneri@lu.unisi.ch
	 * 
	 */

	public PacmanFrame(final Game game) {
		this.game = game;
		setTitle("Pacman");
		setBackground(Color.BLACK);

		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLayout(new BorderLayout());
		makeMenu();

		// Add panels
		gameView = new GameView(game);
		highscoreView = new HighscoreView(game.getHighscore());

		// gameView.addAncestorListener(al);
		// introView.addAncestorListener(al);
		// highscoreView.addAncestorListener(al);

		// cards = new JPanel(new CardLayout());
		// cards.add(gameView, "game");
		// cards.add(highscoreView, "highscore");
		// cards.add(introView, "intro");
		// add(cards, BorderLayout.CENTER);
		// CardLayout cl = (CardLayout) (cards.getLayout());
		// cl.show(cards, "game");

		add(gameView, BorderLayout.CENTER);

		pack();
		setLocationRelativeTo(null);
		setVisible(true);

		firstTimeStart = true;
	}

	private final void makeMenu() {
		final JMenuBar menuBar = new JMenuBar();

		final JMenu fileMenu = new JMenu("File");
		menuBar.add(fileMenu);

		final JMenuItem newGameItem = new JMenuItem("New Game");
		newGameItem.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				gameView.getTimer().stop();
				game.restart();
				gameView.setVisible(false);
				remove(gameView);
				gameView = new GameView(game);
				add(gameView, BorderLayout.CENTER);
				gameView.getBoardView().addListeners();
				gameView.getTimer().start();
				gameView.requestFocus();
				repaint();
			}
		});

		final JMenuItem highscoreItem = new JMenuItem("Highscore");
		highscoreItem.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				
			}
		});
		
		highscoreItem.setEnabled(false);

		final JMenuItem quitItem = new JMenuItem("Quit");
		quitItem.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				quitPacmanGame();
			}
		});

		fileMenu.add(newGameItem);
		fileMenu.add(highscoreItem);
		fileMenu.addSeparator();
		fileMenu.add(quitItem);

		final JMenu helpMenu = new JMenu("Help");
		menuBar.add(helpMenu);

		final JMenuItem controlsItem = new JMenuItem("Controls");
		controlsItem.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				JOptionPane.showMessageDialog(null,
						"Use the arrow keys to move the Pacman.\n"
								+ "Press space to pause/unpause the game.");
			}
		});

		final JMenuItem aboutItem = new JMenuItem("About");
		aboutItem.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				JOptionPane
						.showMessageDialog(null,
								"Pacman\n\nDeveloped by\n\nLuca Vignola & Mark Pruneri.");

			}
		});

		helpMenu.add(controlsItem);
		helpMenu.add(aboutItem);

		setJMenuBar(menuBar);
	}

	private final void quitPacmanGame() {
		if (gameView != null) {
			gameView.getBoardView().removeListeners();
		}
		dispose();
	}

}
